﻿@using StackExchange.Redis
@model StackExchange.Opserver.Data.Redis.RedisInstance
@{
    Layout = null;
    var info = Model;
    var cache = info.Clients;
}
@if (cache != null)
{
    if (cache.ErrorMessage.HasValue())
    {
    <div class="redis-info">
        <div class="last-success-notice">@cache.ErrorMessage</div>
    </div>
    }
    if (cache.LastPoll > cache.LastSuccess)
    {
    <div class="redis-info">
        <div class="last-success-notice">Query failed, showing info from: @cache.LastSuccess.ToRelativeTimeSpanMini()</div>
    </div>
    }
}
@if (cache.Data != null)
{
    <div class="redis-clients">
        @if (!cache.Data.Any())
        {
            <div class="info-line">No Clients</div>
        }
        else
        {
            @Html.Partial("~/Views/Redis/Server.Clients.ByIP.cshtml", info)
            @Html.Partial("~/Views/Redis/Server.Clients.ByName.cshtml", info)
            <div class="info-title">Client Details</div>
            foreach (var ci in cache.Data.OrderByDescending(c => c.Flags).ThenBy(c => c.Host).ThenBy(c => c.AgeSeconds))
            {
                var subCount = ci.SubscriptionCount + ci.PatternSubscriptionCount;
                <div class="info-line" title="@(ci.LastCommand), DB @(ci.Database)"><span class="label">@ci.Address</span> <span class="value">@(string.IsNullOrEmpty(ci.Name) ? "" : ci.Name) @(ci.Flags != ClientFlags.None ? ci.Flags.ToString() : "")</span>
                    @{
                        var idle = DateTime.UtcNow.AddSeconds(-ci.IdleSeconds);
                        <span class="value add-info idle">(@idle.ToRelativeTimeMini(includeTimeForOldDates: false, includeAgo: false))</span>
                    }
                    @if (subCount > 0)
                    {
                        <span class="value add-info subs">(@subCount.ToComma() @("sub".Pluralize(subCount)))</span>
                    }
                </div>
            }
        }
    </div>
}